package com.wx.web.controller.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.eachonline.core.JsonResult;
import com.eachonline.oss.OssConstant;
import com.eachonline.oss.OssService;
import com.eachonline.util.UUIDUtil;
import com.wx.business.util.DateTimeUtil;

@Controller
@RequestMapping("/imgUpload")
public class ImgUploadController {
	
	

	@Autowired
	private OssService ossService;
	
	/**
	 * 上传图片
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/uploadImg", method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult uploadImg(@RequestParam("file") MultipartFile file,
			HttpServletResponse response) {
		
		StringBuffer key = new StringBuffer();
		key.append(DateTimeUtil.formatPureStandardDate(new Date()));
		key.append("/");
		key.append(UUIDUtil.getUUID());
		System.out.println(key.toString());
		ossService.upload(OssConstant.NAMESPACE_BUSINESS,key.toString(), file, "image/jpeg");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("key", key.toString());
		map.put("url", OssConstant.BaseUrl+key.toString());

		return new  JsonResult(true, map);
	}
	
	
	/**
	 * 上传图片
	 * 
	 * @param file
	 * @return
	 * @throws FileUploadException 
	 */
	@RequestMapping(value = "/uploadImge", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String,Object> uploadImgkey( @RequestParam("uploadFile") List<MultipartFile> uploadFile,
			HttpServletResponse response)  {
		  Map<String,Object> resultMap=new HashMap<String, Object>();
		  List<String> urls = new ArrayList<>();
	 
	        for (MultipartFile file : uploadFile) {
	        	StringBuffer key = new StringBuffer();
	    		key.append(DateTimeUtil.formatPureStandardDate(new Date()));
	    		key.append("/");
	    		key.append(UUIDUtil.getUUID());
	    		System.out.println(key.toString());
	    		ossService.upload(OssConstant.NAMESPACE_BUSINESS,key.toString(), file, "image/jpeg");
	    		urls.add(OssConstant.BaseUrl+key.toString());
	        }
	
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		
		resultMap.put("errno", 0);
		resultMap.put("data", urls);
		return resultMap;
	}
}
